package com.hqyj.ssm_project.controller;

import com.hqyj.ssm_project.pojo.Function;
import com.hqyj.ssm_project.pojo.Role;
import com.hqyj.ssm_project.pojo.Role_Function;
import com.hqyj.ssm_project.service.FunctionService;
import com.hqyj.ssm_project.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private FunctionService functionService;

    @RequestMapping(value = "/rolePage.do", method = RequestMethod.GET)
    public String rolePage(Model model) {
//        List<Role> roleList = roleService.selectAllRole();

        List<Role> roleList =roleService.selectRoleAndUser();
        model.addAttribute("roleList", roleList);
        return "rolePage";
    }

    @RequestMapping(value = "/addRolePage.do")
    public String addRolePage() {
        return "addRolePage";
    }

    @RequestMapping(value = "/addRole.do")
    public String addRole(Role role) {
        roleService.insertSelective(role);
        return "redirect:rolePage.do";
    }

    @RequestMapping(value = "/updateRolePage.do")
    public String updateRolePage(Integer id, Model model) {
        Role role = roleService.selectByPrimaryKey(id);
        model.addAttribute("role", role);
        return "updateRolePage";
    }

    @RequestMapping(value = "/updateRole.do")
    public String updateRole(Role role) {
        roleService.updateByPrimaryKeySelective(role);
        return "redirect:rolePage.do";
    }

    @RequestMapping(value = "/deleteRole.do")
    public String deleteRole(Integer id) {
        roleService.deleteFromRole_Function(id);
        roleService.deleteByPrimaryKey(id);
        return "redirect:rolePage.do";
    }

    @RequestMapping(value = "/roleFunctionPage.do")
    public String roleFunctionPage(Model model) {
        List<Role_Function> role_functionList = roleService.selectRole_Function();
        model.addAttribute("role_functionList", role_functionList);
        return "roleFunctionPage";
    }

    @RequestMapping(value = "/addFunctionToRolePage.do")
    public String addFunctionToRolePage(Integer roleId, Model model, HttpServletRequest request) {
        Role role = roleService.selectByPrimaryKey(roleId);
        System.out.println("roleId" + roleId);
        System.out.println("role" + role);
        List<Role_Function> functionByRoleId = functionService.selectFunctionOneTableByRoleId(roleId);
        System.out.println(functionByRoleId);
        List<Function> functionList = functionService.selectAllfunction();
        model.addAttribute("role", role);
        model.addAttribute("functionByRoleId", functionByRoleId);
        HttpSession session = request.getSession();
        session.setAttribute("functionList", functionList);
        session.setAttribute("allFunction", functionList);
        return "addFunctionToRolePage";
    }

    @RequestMapping(value = "/addFunctionToRole.do", method = {RequestMethod.GET})
    public String addFunctionToRole(int[] functionId, Integer roleId) {
        Role_Function role_function = new Role_Function();
        role_function.setRoleId(roleId);
        List<Function> functionList = functionService.selectAllfunction();
        for (Function function : functionList) {
            Integer funId = function.getFunctionId();
            boolean contains = Arrays.asList(functionId).contains(funId);
            if(contains==false){
                role_function.setFunctionId(funId);
                roleService.deleteRole_Function(role_function);
            }
        }
        for (int i : functionId) {
            role_function.setFunctionId(i);
            Role_Function result = roleService.selectRole_FunctionByfunctionIdAndRoleId(role_function);
            if (result == null) {
                roleService.insertRole_Function(role_function);
            }
        }

        return "redirect:roleFunctionPage.do";
    }

    @RequestMapping(value = "/chooseFunction.do")
    public String chooseFunction() {

        return "chooseFunction";
    }

}
